//
//  DFProductsProductAtlasController.m
//  DFront
//
//  Created by lanou on 15/10/27.
//  Copyright © 2015年 xuzenghui. All rights reserved.
//

#import "DFProductsProductAtlasController.h"
#import "PKRequestManager.h"
#import "DFProductsProductAtlasModel.h"
#import "DFProductsProductAtlasCell.h"
#import "UIImageView+WebCache.h"
#import "DFProductsProductAtlasReusableView.h"
#import "DFProductsProductBigPictureController.h"
#import "DFSignlTon.h"
#import "UIBezierPath+ZJText.h"
@interface DFProductsProductAtlasController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,strong)UICollectionViewFlowLayout *flawLayout;
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)NSMutableArray *picArr;
@property (nonatomic,strong) UIImageView *imageView;
@property (strong, nonatomic) NSMutableDictionary *attrs;
@property (strong, nonatomic) CAShapeLayer *shapeLayer;
@end

@implementation DFProductsProductAtlasController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataBaseArr = [NSMutableArray array];
    _picArr = [NSMutableArray array];
    _flawLayout = [[UICollectionViewFlowLayout alloc]init];
    _flawLayout.itemSize = CGSizeMake(kScreenWidth /2 - 0.5,3 * kScreenWidth / 8);
    _flawLayout.minimumInteritemSpacing = 1;
    _flawLayout.minimumLineSpacing = 1;
    _flawLayout.headerReferenceSize = CGSizeMake(100, 50);
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 104, kScreenWidth, kScreenHeight - 104) collectionViewLayout:_flawLayout];
    _collectionView.backgroundColor = [UIColor colorWithWhite:0.906 alpha:1.000];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [_collectionView registerClass:[DFProductsProductAtlasCell class] forCellWithReuseIdentifier:@"co"];
    [_collectionView registerClass:[DFProductsProductAtlasReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ti"];
    
    [self.view addSubview:_collectionView];
    
    [self requestData];
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    // Register cell classes
    // Do any additional setup after loading the view.
}

-(void)requestData{

    NSString *urlString = [NSString stringWithFormat:@"http://lib3.wap.zol.com.cn/index.php?c=Advanced_Picture_V1&proId=%@&subcateId=%@",_proId,_subcateId];
    
    [PKRequestManager requestWithType:GET urlString:urlString parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        
        for (NSDictionary *dict in dic[@"category"]) {
            DFProductsProductAtlasModel *model = [[DFProductsProductAtlasModel alloc]init];
            
            [model setValuesForKeysWithDictionary:dict];
            for (int i = 0; i < model.arr.count; i++) {
                [_picArr addObject:[model.arr[i] bigPic]];
            }
            [self.dataBaseArr addObject:model];
        }
        [self.collectionView reloadData];
        
    } error:^(NSError *error) {
        NSLog(@"界面请求失败==%@",error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.dataBaseArr.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [[self.dataBaseArr[section] arr] count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    DFProductsProductAtlasCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"co" forIndexPath:indexPath];
    DFProductsProductAtlasDetailModel *model = [self.dataBaseArr[indexPath.section] arr][indexPath.item];
    DFSignlTon *signlTon = [DFSignlTon shareInstance];
    if (signlTon.savemode == SaveModeYes) {
        cell.pic.image = [UIImage imageNamed:@""];
    }else
    {
        [cell.pic sd_setImageWithURL:[NSURL URLWithString:model.picSrc]];
    }
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    DFProductsProductAtlasReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ti" forIndexPath:indexPath];
    reusableView.titleLab.text =[@"    " stringByAppendingString:[self.dataBaseArr[indexPath.section] name]];
    return reusableView;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DFProductsProductBigPictureController *bigPicture = [[DFProductsProductBigPictureController alloc]init];
    NSInteger j = 0;
    for (int i = 0; i < indexPath.section; i++) {
        j = j + [[self.dataBaseArr[i] arr] count];
    }
    bigPicture.index = j + indexPath.row;
    bigPicture.picArr = _picArr;
    [self presentViewController:bigPicture animated:YES completion:nil];
}

#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
